<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class StuController extends Controller
{
	//浏览信息
	public function index()
	{
		$data = \DB::table("stu")->get();
		// echo "<pre>";
		// print_r($data);
		// dd($data);
		return view("stu.index",['list'=>$data]);
	}
	
	//加载添加表单
	public function create()
	{
		return view("stu.add");
	}
	
	//执行信息添加
	public function store(Request $request)
	{
		//获取要添加的数据
		$data = ['name'=>$request->name,'age'=>$request->age,'sex'=>$request->sex,'classid'=>$request->classid];
		// dd($data);
		//执行添加
		// $m = \DB::table("stu")->insert($data);
		$m = \DB::table("stu")->insertGetId($data); //返回自增id
		if($m>0){
			return "添加成功; 自增id=".$m;
		}else{
			return "添加失败！";
		}
	}
	
	//单条详情浏览
	public function show($id)
	{
		return "单条详情浏览";
	}
	
	//加载编辑表单
	public function edit($id)
	{
		$data =\DB::table("stu")->where("id",$id)->first();
		return view('stu.edit',['list'=>$data]);
	}
	
	//执行信息编辑(修改)
	public function update(Request $request)
	{	
		$id = $request->id;
		$data = ['name'=>$request->name,'sex'=>$request->sex,'age'=>$request->age,'classid'=>$request->classid];
		
		$res = \DB::table('stu')->where('id',$id)->update($data);
		
		if($res){
			return "修改成功";
		}else{
			return 
			back()->with('error','修改失败');
		}
	}
	
	 //执行信息删除
    public function destroy(Request $request)
    {	
     	$id = $request->input('id');
     	$m = \DB::table("stu")->where('id','=',$id)->delete();
     	if($m>0){
     		return "执行信息删除成功";

     	}else{
     		return "删除失败";
     	}
    	
    }

}